package com.yaodian100.ec.generate.service.impl;

import java.io.File;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.generate.entity.ViewPromotionAdvertise;

public class IdxImageAdvGenerater extends AbstractAdvGenerater {

	private final GenerateServiceImpl generateServiceImpl;

	IdxImageAdvGenerater(GenerateServiceImpl impl) {
		this.generateServiceImpl = impl;
	}

	public boolean generateIdxImageAdv(String advType) throws Exception {
		List<Long> activeAdvIdList = new ArrayList<Long>();

		CommonCriteria criteria = new CommonCriteria();
		criteria.addEq("advType", advType);

		List<ViewPromotionAdvertise> viewPromotionAdvs = generateServiceImpl.getViewPromotionAdvertiseDao()
				.getListPageable(criteria, new String[] { "showSeq asc", "startDate", "advTarget" }, 0, -1);
		if (viewPromotionAdvs != null) {
			String cateId = "IDX";
			boolean mkDirsuccess = generateServiceImpl.mkGenerateHtmlDir(advType, cateId);
			if (!mkDirsuccess) {
				throw new RuntimeException("建立 html 目錄失敗");
			}
			String hppValue = idxImageAdvHpp(advType);
			
			this.generateServiceImpl.deleteFiles(ServerValue.getFrontContentHtmlPath() + cateId, advType);
			
			for (ViewPromotionAdvertise viewPromotionAdv : viewPromotionAdvs) {
				int index = viewPromotionAdvs.indexOf(viewPromotionAdv);
				if (index > 2) {
					break;
				}

				Reader reader = null;
				Properties prop = null;
				
				prop = new Properties();
				reader = new StringReader(viewPromotionAdv.getContents());
				prop.load(reader);
				reader.close();
				
				prop.put("hppValue", hppValue);
				
				prop.put("advType", advType);
				
				String htmlFilePath = defaultAdvHtmlFileFullPath(advType, cateId, index);
				FileUtils.writeStringToFile(new File(htmlFilePath),
						generateServiceImpl.getTemplateService().format("static_IdxImageAdv", prop));
				
				activeAdvIdList.add(Long.valueOf(viewPromotionAdv.getId()));
			}
		}
		
		this.generateServiceImpl.updateAdvStatus(advType, activeAdvIdList);
		return true;
	}

	public boolean generateIdxImageAdv(String domain, String advType, String advType2) throws Exception {
		boolean isIdx = false;
		List<Long> activeAdvIdList = new ArrayList<Long>();

		CommonCriteria criteria = new CommonCriteria();
		criteria.addEq("advType", advType);

		List<ViewPromotionAdvertise> viewPromotionAdvs = generateServiceImpl.getViewPromotionAdvertiseDao()
				.getListPageable(criteria, new String[] { "showSeq asc", "startDate", "advTarget" }, 0, -1);
		
		// 如果advType没资料，抓advType2的
		if( viewPromotionAdvs == null || viewPromotionAdvs.size() == 0 ) {
			isIdx = true;
			criteria = new CommonCriteria();
			criteria.addEq("advType", advType2);
			viewPromotionAdvs = generateServiceImpl.getViewPromotionAdvertiseDao()
			.getListPageable(criteria, new String[] { "showSeq asc", "startDate", "advTarget" }, 0, -1);
		}
		
		if (viewPromotionAdvs != null) {
			// 多一層/pclady.yaodian100.com的folder，但html檔名還是同idx
			String cateId = domain + File.separator + "IDX";
			
			boolean mkDirsuccess = generateServiceImpl.mkGenerateHtmlDir(advType, cateId);
			if (!mkDirsuccess) {
				throw new RuntimeException("建立 html 目錄失敗");
			}
			String hppValue = idxImageAdvHpp(advType);
			
			this.generateServiceImpl.deleteFiles(ServerValue.getFrontContentHtmlPath() + cateId, advType);
			
			for (ViewPromotionAdvertise viewPromotionAdv : viewPromotionAdvs) {
				int index = viewPromotionAdvs.indexOf(viewPromotionAdv);
				if (index > 2) {
					break;
				}

				Reader reader = null;
				Properties prop = null;
				
				prop = new Properties();
				reader = new StringReader(viewPromotionAdv.getContents());
				prop.load(reader);
				reader.close();
				
				prop.put("hppValue", hppValue);
				
				// vm中组图档path会用到
				if( isIdx ) 
					prop.put("advType", advType2);
				else
					prop.put("advType", advType);
				
				String htmlFilePath = defaultAdvHtmlFileFullPath(advType2, cateId, index);
				FileUtils.writeStringToFile(new File(htmlFilePath),
						generateServiceImpl.getTemplateService().format("static_IdxImageAdv", prop).replaceAll("http://www.yaodian100.com", ""));
				
				activeAdvIdList.add(Long.valueOf(viewPromotionAdv.getId()));
			}
		}
		
		this.generateServiceImpl.updateAdvStatus(advType, activeAdvIdList);
		return true;
	}

	private String idxImageAdvHpp(String advType) {
		if (advType.equals("IDX_TOPSPAN")) {
			return "IAdLdr";
		}
		if (advType.equals("IDX_WAISTLINE")) {
			return "IAdMid";
		}
		if (advType.equals("IDX_RIGHTSMALL1")) {
			return "IRtAd_1";
		}
		if (advType.equals("IDX_RIGHTSMALL2")) {
			return "IRtAd_2";
		}
		if (advType.equals("IDX_TWINLEFT")) {
			return "IAdDb1";
		}
		if (advType.equals("IDX_TWINRIGHT")) {
			return "IAdDb2";
		}
		//V5
		if (advType.equals("IDX_HOTSALE_BANNER") || advType.equals("PCL_HOTSALE_BANNER")) {
			return "IHtSale_0";
		}
		if (advType.equals("IDX_BANK_BANNER") || advType.equals("PCL_BANK_BANNER")) {
			return "ICDcard";
		}
		if (advType.equals("IDX_WAISTLINE_V5") || advType.equals("PCL_WAISTLINE_V5")) {
			return "IAdMid";
		}

		return "";
	}

}
